// 无用代码剔除
import { traverse } from "@babel/core";
import { parse } from "@babel/parser";
import _generate from "@babel/generator";
import * as types from "@babel/types";
import fs from "fs";

const code = fs.readFileSync("code3.js", "utf-8");
let ast = parse(code);
traverse(ast, {
   IfStatement(path) {
       let { consequent, alternate } = path.node;
       let testPath = path.get("test");
       const evaluateTest = testPath.evaluateTruthy();
       if (evaluateTest === true) {
           if (types.isBlockStatement(consequent)) {
               consequent = consequent.body;
           }
           path.replaceWithMultiple(consequent);
       } else if (evaluateTest === false) {
           if (alternate != null) {
               if (types.isBlockStatement(alternate)) {
                   alternate = alternate.body;
               }
               path.replaceWithMultiple(alternate);
           } else {
               path.remove();
           }
       }
   }
});

const { code: output } = _generate.default(ast);
console.log(output);